<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'cardd:yfgz:carddYfgz:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="100%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsCarddYfgzCarddYfgzForm">
  import { computed, ref, unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { CarddYfgz, carddYfgzForm, carddYfgzSave } from '/@/api/cardd/yfgz/carddYfgz';
  import { officeTreeData } from '/@/api/sys/office';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('cardd.yfgz.carddYfgz');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<CarddYfgz>({} as CarddYfgz);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增应发工资') : t('编辑应发工资'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('人员编码'),
      field: 'jbgzRybm',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      dynamicDisabled: false,
      ifShow: false,
    },
    {
      label: t('人员名称'),
      field: 'jbgzRymc',
      component: 'TreeSelect',
      componentProps: {
        api: officeTreeData,
        params: { isLoadUser: true, userIdPrefix: '' },
        canSelectParent: false,
        allowClear: true,
        readonly: true,
      },
      dynamicDisabled: false,
    },
    {
      label: t('日期'),
      field: 'yfgzRq',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('基本工资'),
      field: 'jbgzJbgz',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computGrHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
      // colProps: { lg: 12, md: 24 },
    },
    {
      label: t('缴费基数'),
      field: 'jbgzJfjs',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
      // colProps: { lg: 12, md: 24 },
    },
    {
      label: '',
      field: 'detailInfo',
      component: 'Divider',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('养老保险个人'),
      field: 'jbgzYlbxgryj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzYlbxgryj || 0;
            const dwyj = formModel.jbgzYlbxdwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzYlbxxj = xj.toFixed(2);
            } else {
              formModel.jbgzYlbxxj = '';
            }
            computGrHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('养老保险单位'),
      field: 'jbgzYlbxdwyj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzYlbxgryj || 0;
            const dwyj = formModel.jbgzYlbxdwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzYlbxxj = xj.toFixed(2);
            } else {
              formModel.jbgzYlbxxj = '';
            }
            computDwHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('养老保险小计'),
      field: 'jbgzYlbxxj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
      disabled: true,
    },
    {
      label: t('失业险个人'),
      field: 'jbgzSyxgryj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzSyxgryj || 0;
            const dwyj = formModel.jbgzSyxdwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzSyxxj = xj.toFixed(2);
            } else {
              formModel.jbgzSyxxj = '';
            }
            computGrHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('失业险单位'),
      field: 'jbgzSyxdwyj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzSyxgryj || 0;
            const dwyj = formModel.jbgzSyxdwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzSyxxj = xj.toFixed(2);
            } else {
              formModel.jbgzSyxxj = '';
            }
            computDwHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('失业险小计'),
      field: 'jbgzSyxxj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('工伤保险个人'),
      field: 'jbgzGsbxgryj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzGsbxgryj || 0;
            const dwyj = formModel.jbgzGsbxdwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzGsbxxj = xj.toFixed(2);
            } else {
              formModel.jbgzGsbxxj = '';
            }
            computGrHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('工伤保险单位'),
      field: 'jbgzGsbxdwyj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzGsbxgryj || 0;
            const dwyj = formModel.jbgzGsbxdwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzGsbxxj = xj.toFixed(2);
            } else {
              formModel.jbgzGsbxxj = '';
            }
            computDwHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('工伤保险小计'),
      field: 'jbgzGsbxxj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('基本医疗个人'),
      field: 'jbgzJbylgryj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzJbylgryj || 0;
            const dwyj = formModel.jbgzJbyldwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzJbylxj = xj.toFixed(2);
            } else {
              formModel.jbgzJbylxj = '';
            }
            computGrHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('基本医疗单位'),
      field: 'jbgzJbyldwyj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzJbylgryj || 0;
            const dwyj = formModel.jbgzJbyldwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzJbylxj = xj.toFixed(2);
            } else {
              formModel.jbgzJbylxj = '';
            }
            computDwHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('基本医疗小计'),
      field: 'jbgzJbylxj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('大额医疗补助个人'),
      field: 'jbgzDeylbzgryj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzDeylbzgryj || 0;
            const dwyj = formModel.jbgzDeylbzdwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzDeylbzxj = xj.toFixed();
            } else {
              formModel.jbgzDeylbzxj = '';
            }
            computGrHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('大额医疗补助单位'),
      field: 'jbgzDeylbzdwyj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzDeylbzgryj || 0;
            const dwyj = formModel.jbgzDeylbzdwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzDeylbzxj = xj.toFixed(2);
            } else {
              formModel.jbgzDeylbzxj = '';
            }
            computDwHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('大额医疗补助小计'),
      field: 'jbgzDeylbzxj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('生育保险个人'),
      field: 'jbgzSybxgryj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzSybxgryj || 0;
            const dwyj = formModel.jbgzSybxdwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzSybxxj = xj.toFixed(2);
            } else {
              formModel.jbgzSybxxj = '';
            }
            computGrHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('生育保险单位'),
      field: 'jbgzSybxdwyj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            const gryj = formModel.jbgzSybxgryj || 0;
            const dwyj = formModel.jbgzSybxdwyj || 0;
            const xj = Number(gryj) + Number(dwyj);
            if (xj > 0) {
              formModel.jbgzSybxxj = xj.toFixed(2);
            } else {
              formModel.jbgzSybxxj = '';
            }
            computDwHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('生育保险小计'),
      field: 'jbgzSybxxj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('个人应缴合计'),
      field: 'jbgzGryjhj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('单位应缴合计'),
      field: 'jbgzDwyjhj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: '',
      field: 'detailInfo',
      component: 'Divider',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('全勤奖'),
      field: 'jbgzQqj',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('电话补贴'),
      field: 'jbgzDhbt',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('交通补贴'),
      field: 'jbgzJtbt',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('洗车补贴'),
      field: 'jbgzXcbt',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('无违章投诉补贴'),
      field: 'jbgzWwztsbt',
      component: 'Input',
      componentProps: ({ formModel }) => {
        return {
          maxlength: 8,
          onInput: async () => {
            computZHjje(formModel);
          },
        };
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('合计'),
      field: 'jbgzHj',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 8, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await carddYfgzForm(data);
    record.value = (res.carddYfgz || {}) as CarddYfgz;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await carddYfgzSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }

  function computGrHjje(formModel: any) {
    const hjje = [
      'jbgzYlbxgryj',
      'jbgzSyxgryj',
      'jbgzGsbxgryj',
      'jbgzJbylgryj',
      'jbgzDeylbzgryj',
      'jbgzSybxgryj',
    ].reduce((prev, key) => {
      return Number(prev) + Number(formModel[key] || 0);
    }, 0);
    formModel.jbgzGryjhj = hjje.toFixed(2);
    computZHjje(formModel);
  }

  function computDwHjje(formModel: any) {
    const hjje = [
      'jbgzYlbxdwyj',
      'jbgzSyxdwyj',
      'jbgzGsbxdwyj',
      'jbgzJbyldwyj',
      'jbgzDeylbzdwyj',
      'jbgzSybxdwyj',
    ].reduce((prev, key) => {
      return Number(prev) + Number(formModel[key] || 0);
    }, 0);
    formModel.jbgzDwyjhj = hjje.toFixed(2);
    computZHjje(formModel);
  }

  function computZHjje(formModel: any) {
    const hjje = [
      'jbgzJbgz',
      'jbgzYlbxxj',
      'jbgzSyxxj',
      'jbgzGsbxxj',
      'jbgzJbylxj',
      'jbgzDeylbzxj',
      'jbgzSybxxj',
      'jbgzQqj',
      'jbgzDhbt',
      'jbgzJtbt',
      'jbgzXcbt',
      'jbgzWwztsbt',
    ].reduce((prev, key) => {
      return Number(prev) + Number(formModel[key] || 0);
    }, 0);
    formModel.jbgzHj = hjje.toFixed(2);
  }
</script>
